package com.ridaizu.bean;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.google.common.base.Strings;
import com.ridaizu.cache.CacheDataBean;
import com.ridaizu.dao.db.DBManager;
import com.ridaizu.lazy.LazyTransitDetailDataModel;
import com.ridaizu.model.TransitDetail;
import com.ridaizu.model.form.TransitDetailForm;
import com.ridaizu.model.search.TransitDetailSearch;
import com.ridaizu.service.impl.TransitDetailServiceImpl;

@javax.faces.bean.ManagedBean(name = "transitDetailBean")
@SessionScoped
public class TransitDetailBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private LazyDataModel<TransitDetail> lazyModel;

    @ManagedProperty("#{cacheDataBean}")
    private CacheDataBean cacheDataBean;

    @ManagedProperty("#{transitDetailServiceImpl}")
    private TransitDetailServiceImpl transitDetailService;

    @ManagedProperty("#{transitDetailSearch}")
    private TransitDetailSearch transitDetailSearch;

    @ManagedProperty("#{transitDetailForm}")
    private TransitDetailForm transitDetailForm;

	public void searchTransitDetailSync() {
		lazyModel = new LazyTransitDetailDataModel(transitDetailSearch);
	}

	public void viewRegisterTransitDetail() throws SQLException {

		setInitValues(null);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("contentWidth", 550);
        options.put("contentHeight", 200);

        RequestContext.getCurrentInstance().openDialog("transit-detail", options, null);
    }

	public void viewUpdateTransitDetail(TransitDetail transitDetail) throws SQLException {

		setInitValues(transitDetail);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("contentWidth", 550);
        options.put("contentHeight", 220);

        RequestContext.getCurrentInstance().openDialog("transit-detail", options, null);
    }

	private void setInitValues(TransitDetail transitDetail) throws SQLException {
		transitDetailForm.clearAll();
		setValues(transitDetail);
	}

	private void setValues(TransitDetail transitDetail) {
		if (transitDetail == null) {
			setRegisterValues();
		} else {
			setUpdateValues(transitDetail);
		}
	}

	private void setRegisterValues() {
	}

	private void setUpdateValues(TransitDetail transitDetail) {
		transitDetailForm.setId(String.valueOf(transitDetail.getId()));
		transitDetailForm.setTransitId(String.valueOf(transitDetail.getTransitId()));
		transitDetailForm.setGoodsId(String.valueOf(transitDetail.getGoodsId()));
		transitDetailForm.setCount(String.valueOf(transitDetail.getCount()));
		transitDetailForm.setPrice(String.valueOf(transitDetail.getPrice()));
	}

	public void registerTransitDetail() {
		TransitDetail transitDetail = getTransitDetail();

		boolean result = false;

		try {
			DBManager.beginTransaction();
			result = transitDetailService.insertModel(transitDetail);
			DBManager.commitTransaction();

		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		RequestContext.getCurrentInstance().closeDialog(result);
    }

	public void updateTransitDetail() {

		TransitDetail transitDetail = getTransitDetail();

		boolean result = false;

		try {
			DBManager.beginTransaction();
			result = transitDetailService.updateModel(transitDetail);
			DBManager.commitTransaction();

		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		RequestContext.getCurrentInstance().closeDialog(result);
	}

	public void deleteTransitDetail() {

		TransitDetail selectedTransitDetail = transitDetailForm.getSelectedTransitDetail() ;

		if (selectedTransitDetail == null || selectedTransitDetail.getId() < 1) {
	        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", "请选择要删除的在途详细信息。");
	        FacesContext.getCurrentInstance().addMessage(null, message);

	        return ;
		}

		boolean result = false;
		try {
			DBManager.beginTransaction();
			result = transitDetailService.deleteModel(selectedTransitDetail.getId());
			DBManager.commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		String resultStr = null;
		if (result) {
			resultStr = String.format("在途详细[%s]删除成功。", selectedTransitDetail.getId());
		} else {
			resultStr = String.format("在途详细[%s]删除失败。", selectedTransitDetail.getId());
		}

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "TransitDetail delete result", resultStr);
        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void registerTransitDetailResult(SelectEvent event) {
		boolean result = (Boolean) event.getObject();

		handleResult(result, "登录");
	}

	public void updateTransitDetailResult(SelectEvent event) {

		boolean result = (Boolean) event.getObject();

		handleResult(result, "更新");
	}

	private void handleResult(boolean result, String optStr) {

		String resultStr = null;
		if (result) {
			resultStr = String.format("在途详细信息%s成功。", optStr);
		} else {
			resultStr = String.format("在途详细信息%s失败。", optStr);;
		}

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", resultStr);

        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	private TransitDetail getTransitDetail() {

		TransitDetail transitDetail = new TransitDetail();

		if (!Strings.isNullOrEmpty(transitDetailForm.getId())) {
			transitDetail.setId(Integer.valueOf(transitDetailForm.getId()));
		}

		if (!Strings.isNullOrEmpty(transitDetailForm.getTransitId())) {
			transitDetail.setTransitId(Integer.valueOf(transitDetailForm.getTransitId()));
		}

		if (!Strings.isNullOrEmpty(transitDetailForm.getGoodsId())) {
			transitDetail.setGoodsId(Integer.valueOf(transitDetailForm.getGoodsId()));
		}

		if (!Strings.isNullOrEmpty(transitDetailForm.getCount())) {
			transitDetail.setCount(Integer.valueOf(transitDetailForm.getCount()));
		}

		if (!Strings.isNullOrEmpty(transitDetailForm.getPrice())) {
			transitDetail.setPrice(Double.valueOf(transitDetailForm.getPrice()));
		}

		return transitDetail;
	}

	public LazyDataModel<TransitDetail> getLazyModel() {
		return lazyModel;
	}

	public void setLazyModel(LazyDataModel<TransitDetail> lazyModel) {
		this.lazyModel = lazyModel;
	}

	public TransitDetailServiceImpl getTransitDetailService() {
		return transitDetailService;
	}

	public void setTransitDetailService(TransitDetailServiceImpl transitDetailService) {
		this.transitDetailService = transitDetailService;
	}

	public TransitDetailSearch getTransitDetailSearch() {
		return transitDetailSearch;
	}

	public void setTransitDetailSearch(TransitDetailSearch transitDetailSearch) {
		this.transitDetailSearch = transitDetailSearch;
	}

	public TransitDetailForm getTransitDetailForm() {
		return transitDetailForm;
	}

	public void setTransitDetailForm(TransitDetailForm transitDetailForm) {
		this.transitDetailForm = transitDetailForm;
	}

	public CacheDataBean getCacheDataBean() {
		return cacheDataBean;
	}

	public void setCacheDataBean(CacheDataBean cacheDataBean) {
		this.cacheDataBean = cacheDataBean;
	}

}